<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="yaml.constants.html">« 预定义常量</a></li>
      <li style="float: right;"><a href="yaml.callbacks.html">Callbacks »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.yaml.html">Yaml</a></li>
    <li>范例</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="yaml.examples" class="chapter">
 <h1>范例</h1>

 <div class="example" id="example-3853">
  <p><strong>示例 #1 Yaml 范例</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$addr&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"given"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Chris"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"family"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Dumars"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"address"</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"lines"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"458&nbsp;Walkman&nbsp;Dr.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Suite&nbsp;#292"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"city"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Royal&nbsp;Oak"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"state"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"MI"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"postal"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">48046</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />&nbsp;&nbsp;);<br /></span><span style="color: #0000BB">$invoice&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"invoice"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">34843</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"date"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"2001-01-23"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"bill-to"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$addr</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"ship-to"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$addr</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"product"</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"sku"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"BL394D"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"quantity"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"description"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Basketball"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"price"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">450</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"sku"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"BL4438H"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"quantity"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"description"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Super&nbsp;Hoop"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"price"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2392</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"tax"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">251.42</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"total"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">4443.52</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"comments"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Late&nbsp;afternoon&nbsp;is&nbsp;best.&nbsp;Backup&nbsp;contact&nbsp;is&nbsp;Nancy&nbsp;Billsmer&nbsp;@&nbsp;338-4338."</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;);<br /><br /></span><span style="color: #FF8000">//&nbsp;把该&nbsp;invoice&nbsp;转换生成&nbsp;YAML&nbsp;的表示法<br /></span><span style="color: #0000BB">$yaml&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">yaml_emit</span><span style="color: #007700">(</span><span style="color: #0000BB">$invoice</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$yaml</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;把&nbsp;YAML&nbsp;转换成&nbsp;PHP&nbsp;变量<br /></span><span style="color: #0000BB">$parsed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">yaml_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$yaml</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;来回转换，以检查两个结构是否相等效<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$parsed&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$invoice</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

  <div class="example-contents"><p>以上例程的输出类似于：</p></div>
  <div class="example-contents screen">
<div class="cdata"><pre>
string(631) &quot;---
invoice: 34843
date: &quot;2001-01-23&quot;
bill-to:
  given: Chris
  family: Dumars
  address:
    lines: |-
      458 Walkman Dr.
              Suite #292
    city: Royal Oak
    state: MI
    postal: 48046
ship-to:
  given: Chris
  family: Dumars
  address:
    lines: |-
      458 Walkman Dr.
              Suite #292
    city: Royal Oak
    state: MI
    postal: 48046
product:
- sku: BL394D
  quantity: 4
  description: Basketball
  price: 450
- sku: BL4438H
  quantity: 1
  description: Super Hoop
  price: 2392
tax: 251.420000
total: 4443.520000
comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.
...
&quot;
bool(true)
</pre></div>
  </div>
 </div>
</div>
</div></div></body></html>